package com.tms.service.impl;

import com.tms.domain.TDef;
import com.tms.domain.Tool;
import com.tms.service.AlgorithmService;
import com.tms.service.RepairRecordService;
import com.tms.service.UseRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class AlgorithmServiceImpl implements AlgorithmService {

    @Autowired
    private UseRecordService useRecordService;

    @Autowired
    private RepairRecordService RepairRecordService;

    @Override
    public double simpleProbability(Tool tool) {
        //入库日期，计算点检周期的时候要用到
        Date regDate = tool.getRegDate();
        Date currentDate = new Date();
        TDef tDef = tool.getTDef();
        int pmPeriod = tDef.getPmPeriod();
        int useDay = (int) (currentDate.getTime() - regDate.getTime()) / (24 * 60 * 60 * 1000);
        int pmNum = useDay / pmPeriod;
        int usedCount = tool.getUsedCount();
        double src = reflectPM(pmNum);
        double probability = sigmoid(src);
        return probability;
    }

    /**
     * pmPeriod为30的情况下，假设寿命为900天
     */
    public static double reflectPM(int x) {
        return (double) x / 3.0 - 5;
    }

    /**
     * usedCount的映射
     */
    public static double reflectUsedCount(int x) {
        return (double) x / 100.0 - 5;
    }

    public static double sigmoid(double src) {
        return 1.0 / (1 + Math.exp(-src));
    }
}
